﻿namespace ElisVK.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    public class DirectoryControl : Label
    {
        #region Fields

        public static readonly RoutedEvent BrowseEvent =
           EventManager.RegisterRoutedEvent(
               "Browse",
               RoutingStrategy.Bubble,
               typeof(RoutedEventHandler),
               typeof(DirectoryControl));

        private static readonly DependencyPropertyKey _isMouseLeftButtonDownPropertyKey =
            DependencyProperty.RegisterReadOnly(
                "IsMouseLeftButtonDown",
                typeof(bool),
                typeof(DirectoryControl),
                new PropertyMetadata());

        private static readonly DependencyProperty _isMouseLeftButtonDownProperty = _isMouseLeftButtonDownPropertyKey.DependencyProperty;

        #endregion Fields

        #region Constructors

        static DirectoryControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(DirectoryControl),
                new FrameworkPropertyMetadata(typeof(DirectoryControl)));
        }

        #endregion Constructors

        #region properties

        public event RoutedEventHandler Browse
        {
            add
            {
                AddHandler(BrowseEvent, value);
            }

            remove
            {
                RemoveHandler(BrowseEvent, value);
            }
        }

        public bool IsMouseLeftButtonDown
        {
            get
            {
                return (bool)GetValue(_isMouseLeftButtonDownProperty);
            }

            private set
            {
                SetValue(_isMouseLeftButtonDownPropertyKey, value);
            }
        }

        #endregion properties

        #region Methods

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var iconBorder = GetTemplateChild("PART_IconBorder") as Border;

            if (iconBorder != null)
            {
                iconBorder.MouseLeftButtonDown += IconBorder_MouseLeftButtonDown;
                iconBorder.MouseLeftButtonUp += IconBorder_MouseLeftButtonUp;
                iconBorder.MouseLeave += IconBorder_MouseLeave;
                iconBorder.KeyDown += IconBorder_KeyDown;
            }
        }

        private void DoBrowse(object sender)
        {
            var args = new RoutedEventArgs(BrowseEvent);

            RaiseEvent(args);

            var uiElement = sender as UIElement;

            if (uiElement != null)
                uiElement.Focus();
        }

        private void IconBorder_MouseLeftButtonDown(object obj, MouseButtonEventArgs e)
        {
            IsMouseLeftButtonDown = true;
        }

        private void IconBorder_MouseLeftButtonUp(object obj, MouseButtonEventArgs e)
        {
            if (!IsMouseLeftButtonDown)
                return;

            DoBrowse(obj);
            IsMouseLeftButtonDown = false;
        }

        private void IconBorder_MouseLeave(object obj, MouseEventArgs e)
        {
            IsMouseLeftButtonDown = false;
        }

        private void IconBorder_KeyDown(object obj, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
                DoBrowse(obj);
        }

        #endregion Methods
    }
}
